using UnityEngine;
using System.Collections;

public class HardPaddle : MonoBehaviour
{
	public int moveSpeed = 140;  //per second
	public static int lifeCheck;
	public Transform mPaddle;
	public Transform fPaddle;
	public Transform newFrontPaddle;
	public Transform newMiddlePaddle;
	public AudioClip ExtraLife;
	public AudioClip Explosion;
	public GameObject FrontPaddle;
	public GameObject MiddlePaddle;
	public GameObject BackPaddle;
	
	// Use this for initialization
    void Start()
    {
		Bullet.score = 0;
    }

    // Update is called once per frame
    void Update()
    {
            PlayerMove();
			LoseLife();
			GainLife();
    }

    void PlayerMove()
    {
        Vector3 moveDirection = Vector3.zero;
        Vector3 newPosition = Vector3.zero;

        if (Input.GetKey(KeyCode.UpArrow)) moveDirection.y += 1;
        if (Input.GetKey(KeyCode.DownArrow)) moveDirection.y -= 1;

        moveDirection = moveDirection * (moveSpeed * Time.deltaTime);

        newPosition = transform.position + moveDirection;

        newPosition.y = Mathf.Clamp(newPosition.y, -88, 90);

        transform.position = newPosition;
    }
	
	void LoseLife()
	{
		if(Bullet.stop)
		{
			if(FrontPaddle.collider.enabled == true)
			{
				FrontPaddle.collider.enabled = false;
				FrontPaddle.renderer.enabled = false;
				audio.PlayOneShot(Explosion);
				Bullet.stop = false;
			}
			
			else if (MiddlePaddle.collider.enabled == true)
			{
				MiddlePaddle.collider.enabled = false;
				MiddlePaddle.renderer.enabled = false;
				audio.PlayOneShot(Explosion);
				Bullet.stop = false;
			}
			
			else
			{
				BackPaddle.renderer.enabled = false;
				BackPaddle.collider.enabled = false;
				audio.PlayOneShot(Explosion);
				AddScore(HardBullet.score);
				Application.LoadLevel("HighScore");
			}
		}
	}
	
	void GainLife ()
	{
		if((Bullet.score / 1000) > lifeCheck)
		{
			if(MiddlePaddle.renderer.enabled == false)
		    {
				MiddlePaddle.collider.enabled = true;
				MiddlePaddle.renderer.enabled = true;
				audio.PlayOneShot(ExtraLife);
				lifeCheck++;
			}
			
			else if(FrontPaddle.renderer.enabled == false)
		    {
				FrontPaddle.renderer.enabled = true;
				FrontPaddle.collider.enabled = true;
				audio.PlayOneShot(ExtraLife);
				lifeCheck++;
			}
			
			else lifeCheck++;
		}
	}
	
	void AddScore(int score)
	{
		int myScores;
		int tempScore;
		string myName;
		string tempName;
		for (int i = 0; i < 10; i++)
		{
  			myScores = PlayerPrefs.GetInt("Scores"+i);
			myName = PlayerPrefs.GetString("Names"+i);
			if(score > myScores)
			{
				tempScore = myScores;
				myScores = score;
				score = tempScore;
				tempName = myName;
				myName = DifficultyOption.pName;
				DifficultyOption.pName = tempName;
			}
			PlayerPrefs.SetInt("Scores"+i, myScores);
			PlayerPrefs.SetString("Names"+i, myName);
			
		}
		
		PlayerPrefs.Save();
	}
	
}
